#include "T_GridScene.h"
#include <QPainter>

T_GridScene::T_GridScene(QObject *parent)
{
    mGridSize = 10;
    mGridColor = Qt::gray;
    mGridVisible = true;
}

T_GridScene::~T_GridScene()
{

}

void T_GridScene::setGridVisible(bool gridVisible)
{
    if (mGridVisible != gridVisible)
    {
        mGridVisible = gridVisible;
        update();
    }
}

void T_GridScene::drawBackground(QPainter *painter, const QRectF &rect)
{
    if (mGridVisible)
    {
        painter->setPen(QPen(mGridColor,0,Qt::SolidLine));
        QRect r = rect.toRect();
        int xmin = r.left() - r.left() % mGridSize - mGridSize;
        int ymin = r.top() - r.top() % mGridSize - mGridSize;
        int xmax = r.right() - r.right() % mGridSize + mGridSize;
        int ymax = r.bottom() - r.bottom() % mGridSize + mGridSize;
        for (int x = xmin; x <= xmax; x += mGridSize)
        {
            painter->drawLine(x, r.top(), x, r.bottom());
        }

        for (int y = ymin; y <= ymax; y += mGridSize)
        {
            painter->drawLine(r.left(), y, r.right(), y);
        }
    } 
}